package com.njcb.game.service;

import com.njcb.ams.factory.comm.DataBus;
import com.njcb.ams.pojo.enumvalue.JobResult;
import com.njcb.ams.scheduler.bean.BaseJobDetail;
import com.njcb.ams.util.AmsJsonUtils;
import com.njcb.ams.util.AmsUtils;
import com.njcb.game.pojo.CheckResult;
import com.njcb.game.pojo.CheckType;
import com.njcb.game.repository.dao.GameCheckDAO;
import com.njcb.game.repository.dao.GameQuestionsDAO;
import com.njcb.game.repository.dao.GameRegisterDAO;
import com.njcb.game.repository.dao.GameResultDAO;
import com.njcb.game.repository.entity.*;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author LOONG
 */
public class GameCheckTask extends BaseJobDetail {
    private static final Logger logger = LoggerFactory.getLogger(GameCheckTask.class);
    @Autowired
    private GameRegisterDAO registerDAO;
    @Autowired
    private GameQuestionsDAO questionsDAO;
    @Autowired
    private GameCheckDAO checkDAO;
    @Autowired
    private GameResultDAO resultDAO;
    @Autowired
    private QuestionsCheckService checkService;

    @Override
    public JobResult executeJob(JobExecutionContext context) {
        Map<String, Object> dataMap = context.getJobDetail().getJobDataMap();
        List<GameRegister> registerList = registerDAO.selectByExample(new GameRegisterExample());
        for (GameRegister gameRegister : registerList) {
            Map paramMap = new HashMap<>();
            paramMap.putAll(AmsJsonUtils.objectToMap(gameRegister));
            DataBus.setAttribute("paramMap", paramMap);
            List<GameQuestions> questionsList = questionsDAO.selectByExample(new GameQuestionsExample());
            for (GameQuestions gameQuestions : questionsList) {
                DataBus.getAttribute("paramMap", Map.class).putAll(AmsJsonUtils.objectToMap(gameQuestions));
                if (CheckType.URL.getCode().equals(gameQuestions.getCheckType())) {
                    //调用URL
                    if (AmsUtils.isNull(gameRegister.getServerAddr())) {
                        continue;
                    }
                    String fullUrl = gameRegister.getServerAddr();
                    if (!fullUrl.substring(fullUrl.length() - 1).equals("/")) {
                        fullUrl = fullUrl + "/";
                    }
                    fullUrl = fullUrl + gameQuestions.getCheckUrl();
                    checkService.checkQuestion(fullUrl, gameQuestions.getCheckParam());
                } else if (CheckType.SQL.getCode().equals(gameQuestions.getCheckType())) {
                    List rt = checkDAO.executeSql(gameQuestions.getCheckSql(), DataBus.getAttribute("paramMap", Map.class));
                    if (rt.size() > 0) {
                        checkSucc(gameRegister, gameQuestions);
                    } else {
                        checkFail(gameRegister, gameQuestions);
                    }
                }
            }
        }
        return JobResult.RESULT_SUCCESS;
    }

    private void checkFail(GameRegister gameRegister, GameQuestions gameQuestions) {
        GameResult result = resultDAO.selectByQustId(gameQuestions.getId());
        if (null == result) {
            result = new GameResult();
        }
        result.setRunnersNo(gameRegister.getRunnersNo());
        result.setRunnersName(gameRegister.getRunnersName());
        result.setQustId(gameQuestions.getId());
        result.setQuesNo(gameQuestions.getQuesNo());
        result.setQuesTitle(gameQuestions.getQuesTitle());
        result.setScore(gameQuestions.getScore());
        result.setCheckUrl(gameQuestions.getCheckUrl());
        result.setCheckResult(CheckResult.FAIL.getCode());
        result.setRunnersNo(gameRegister.getRunnersNo());
        result.setRunnersName(gameRegister.getRunnersName());
        resultDAO.insert(result);
    }

    /**
     * 验证成功处理
     *
     * @param gameRegister
     * @param gameQuestions
     */
    private void checkSucc(GameRegister gameRegister, GameQuestions gameQuestions) {
        GameResult result = resultDAO.selectByQustId(gameQuestions.getId());
        if (null == result) {
            result = new GameResult();
        }
        result.setRunnersNo(gameRegister.getRunnersNo());
        result.setRunnersName(gameRegister.getRunnersName());
        result.setQustId(gameQuestions.getId());
        result.setQuesNo(gameQuestions.getQuesNo());
        result.setQuesTitle(gameQuestions.getQuesTitle());
        result.setScore(gameQuestions.getScore());
        result.setCheckUrl(gameQuestions.getCheckUrl());
        result.setCheckResult(CheckResult.SUCC.getCode());
        result.setRunnersNo(gameRegister.getRunnersNo());
        result.setRunnersName(gameRegister.getRunnersName());
        resultDAO.insert(result);
    }

    @Override
    public void afterReturning(JobExecutionContext context, JobResult result) throws Exception {
        logger.info("执行结果:" + result);
        logger.info("测试传输值[{}]", context.getJobDetail().getJobDataMap().get("testkey"));
    }

}